package com.david.course_design.shopping_center.module.shoppingCart.controller;

import com.david.course_design.shopping_center.module.shoppingCart.entity.ShoppingCart;
import com.david.course_design.shopping_center.module.shoppingCart.entity.VO.ShoppingCartItemVo;
import com.david.course_design.shopping_center.module.shoppingCart.entity.VO.ShoppingCartVo;
import com.david.course_design.shopping_center.module.shoppingCart.service.ShoppingCartService;
import com.david.course_design.shopping_center.result.PageObject;
import com.david.course_design.shopping_center.result.ResultBody;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@ResponseBody
@Api(tags = "购物车接口")
@RequestMapping("ShoppingCart/")
public class ShoppingCartController {
    // 依赖注入(DI) dependency injection
    @Resource
    private ShoppingCartService shoppingCartService;

    @ApiOperation("查询当前用户购物车中的所有商品信息（分页）")
    @GetMapping("CurrentUserSelectAllShoppingCartItemByPaging")
    private ResultBody CurrentUserSelectAllShoppingCartItemByPaging(Integer userId, PageObject pageObject) {

        PageInfo<ShoppingCart> shoppingCartPageInfo = shoppingCartService.CurrentUserSelectAllShoppingCartItemByPaging(userId, pageObject);

        if (shoppingCartPageInfo != null) {

            return ResultBody.success(shoppingCartPageInfo);

        }

        return ResultBody.error("数据请求失败，请稍后重试！");
    }

    @ApiOperation("添加商品到当前用户的购物车中")
    @PostMapping("addGoodsItemToCurrentUserShoppingCartByGoodsId")
    private ResultBody addGoodsItemToCurrentUserShoppingCartByGoodsId(@RequestBody ShoppingCart shoppingCart) {
        /**
         * addGoodsItemToCurrentUserShoppingCartByGoodsId 返回值的解释：
         * 1： 插入数据库成功
         * 0： 插入数据库失败
         * -1： 数据已经在数据库中
         *
         */
        Integer result = shoppingCartService.addGoodsItemToCurrentUserShoppingCartByGoodsId(shoppingCart.getUserId(), new ShoppingCartVo(shoppingCart.getGoodsId(), shoppingCart.getGoodsNumber()));

        if (result == 1) {

            return ResultBody.success("YES");

        } else if (result == -1) {

            return ResultBody.error("-1");

        }

        return ResultBody.error("NO");

    }

    @ApiOperation("从当前用户的购物车中删除商品")
    @DeleteMapping("deleteGoodsItemFromCurrentUserShoppingCartByGoodsId")
    private ResultBody deleteGoodsItemFromCurrentUserShoppingCartByGoodsId(Integer userId, Integer goodsId) {

        Integer result = shoppingCartService.deleteGoodsItemFromCurrentUserShoppingCartByGoodsId(userId, goodsId);

        if (result == 1) {

            return ResultBody.success("YES");

        }

        return ResultBody.error("NO");
    }

    @ApiOperation("通过用户主键和商品主键修改商品的数量")
    @PutMapping("updateShoppingCartItemGoodsNumberByUserIdAndGoodsId")
    private ResultBody updateShoppingCartItemGoodsNumberByUserIdAndGoodsId(@RequestBody ShoppingCart shoppingCart) {
        // System.out.println("userId ==> " + userId + "\ngoodsId ==> " + goodsId + "\ngoodsNumber ==> " + goodsNumber);

        Integer result = shoppingCartService.updateShoppingCartItemGoodsNumberByUserIdAndGoodsId(shoppingCart.getUserId(), shoppingCart.getGoodsId(), Integer.parseInt(shoppingCart.getGoodsNumber()));

        return ResultBody.success(result);
    }

    @ApiOperation("通过用户的主键和分页对象查询当前用户的购物车中的商品数量")
    @GetMapping("selectShoppingCartItemByPaging")
    private ResultBody selectShoppingCartItemByPaging(Integer userId, PageObject pageObject) {
        PageInfo<ShoppingCartItemVo> shoppingCartItemVoPageInfo = shoppingCartService.selectShoppingCartItemByPaging(userId, pageObject);

        if (shoppingCartItemVoPageInfo != null) {
            //  返回的数据不为空，将数据传回前端
            return ResultBody.success(shoppingCartItemVoPageInfo);
        } else {
            //  返回的数据为空，提示出错了
            return ResultBody.error("No");
        }
    }
}
